package com.metalrain.alm.service;

import android.util.Log;
import com.philips.lighting.hue.sdk.PHAccessPoint;
import com.philips.lighting.hue.sdk.PHMessageType;
import com.philips.lighting.hue.sdk.PHSDKListener;
import com.philips.lighting.model.PHBridge;
import com.philips.lighting.model.PHGroup;
import com.philips.lighting.model.PHLight;

import java.util.List;

/**
* Created by administrator on 2014-06-14.
*/
class MyPHSDKListener implements PHSDKListener {
    private HueManager hueManager;
    public MyPHSDKListener(HueManager hueManager) {
        this.hueManager = hueManager;
    }


    @Override
    public void onCacheUpdated(int i, PHBridge phBridge) {
    }

    @Override
    public void onBridgeConnected(PHBridge phBridge) {
        Log.i("HueManager", "Bridge Connected: " + phBridge);
        hueManager.phHueSDK.setSelectedBridge(phBridge);
        hueManager.service.setState(LCA_Service.STATES.CONNECTED_TO_BRIDGE);

        //Show Groups
        Log.i("HueManager", "onBridgeConnected: Debugging Group info");
        for (PHGroup group:hueManager.phHueSDK.getSelectedBridge().getResourceCache().getAllGroups()) {
            Log.i("HueManager", "onBridgeConnected: "+group);
        }
        Log.i("HueManager", "onBridgeConnected: Debugging Light info");
        for (PHLight light:hueManager.phHueSDK.getSelectedBridge().getResourceCache().getAllLights()) {
            Log.i("HueManager", "onBridgeConnected: "+light);
        }
    }

    @Override
    public void onAuthenticationRequired(PHAccessPoint phAccessPoint) {
        Log.i("HueManager", "Authentication Required: " + phAccessPoint);
        hueManager.service.setState(LCA_Service.STATES.AUTHENTICATING);
        hueManager.phHueSDK.startPushlinkAuthentication(phAccessPoint);


    }

    @Override
    public void onAccessPointsFound(List<PHAccessPoint> phAccessPoints) {
        Log.i("HueManager", "Access Points Found: " + phAccessPoints);

        if (hueManager.phHueSDK.getSelectedBridge() != null) return;
        phAccessPoints.get(0).setUsername("CameraControl");
        hueManager.phHueSDK.connect(phAccessPoints.get(0));
    }

    @Override
    public void onError(int i, String s) {
        if (i == 42) {

            throw new IllegalStateException("Error 42 "+s);

        }
        Log.i("HueManager", "onError: "+i+" "+s);
        if (i == PHMessageType.PUSHLINK_BUTTON_NOT_PRESSED) return;
        hueManager.service.setErrorMessage(s);
        hueManager.service.setState(LCA_Service.STATES.ERROR);
    }

    @Override
    public void onConnectionResumed(PHBridge phBridge) {
        Log.i("HueManager", "Connection Resumed: " + phBridge);
        hueManager.phHueSDK.setSelectedBridge(phBridge);
    }

    @Override
    public void onConnectionLost(PHAccessPoint phAccessPoint) {
        Log.i("HueManager", "Connection lost: " + phAccessPoint);
        hueManager.phHueSDK.setSelectedBridge(null);
        hueManager.service.setErrorMessage("Connection Lost");
        hueManager.service.setState(LCA_Service.STATES.ERROR);
    }
}
